package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DAO.*;
import model.*;

public class QueStudy_QuestController extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try {
			processRequest(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String uri = request.getRequestURI();
	
		String command = uri.substring(uri.lastIndexOf("/") + 1);

		ActionForward forward = null;
		Action action = null;

	
		 if (command != null && command.trim().equals("New.Quest")){
			 
			 HttpSession session=request.getSession();
				
			
				
				int studyId = request.getParameter("studyId") != null
						&& request.getParameter("studyId") != "" ? Integer
						.parseInt(request.getParameter("studyId")) : 0;
				int userIdCheck = session.getAttribute("userid") != null
						&& session.getAttribute("userid") != "" ? Integer
						.parseInt((String)session.getAttribute("userid")) : 0;
						
				
				System.out.println(studyId);
				if (userIdCheck < 1) {
					session.invalidate();
					Util.ShowError(response, "로그인해주세요");
				}
				if (studyId < 1) {
					Util.ShowError(response, "잘못 된 접근입니다");
				}
				
			request.setAttribute("studyId", studyId);	
		
			
			forward =  new ActionForward();
			forward.setPath("/study_quest_makeQuest.jsp");
			
		}
		 else if (command != null && command.trim().equals("Modify.Quest")){
			 HttpSession session=request.getSession();
				Quest quest = new Quest();
				QuestDao questDao = new QuestDao();
			
				int studyId = request.getParameter("studyId") != null
						&& request.getParameter("studyId") != "" ? Integer
						.parseInt(request.getParameter("studyId")) : 0;
				int userIdCheck = session.getAttribute("userid") != null
						&& session.getAttribute("userid") != "" ? Integer
						.parseInt((String)session.getAttribute("userid")) : 0;
				if (userIdCheck < 1) {
					session.invalidate();
					Util.ShowError(response, "로그인해주세요");
				}
				if (studyId < 1) {
					Util.ShowError(response, "잘못 된 접근입니다");
				}
				

				int questId = request.getParameter("questId") != null
						&& request.getParameter("questId") != "" ? Integer
								.parseInt(request.getParameter("questId")) : 0;
				if(questId>0)
				{
					quest=questDao.getQuestInfo(questId);
					request.setAttribute("quest", quest);
				}
				
				request.setAttribute("studyId", studyId);	
			forward =  new ActionForward();
			forward.setPath("/study_quest_modifyQuest.jsp");
			
		}
		
		else if (command != null && command.trim().equals("NewAction.Quest")){
			action = new QuestNewAction();
			try {

				forward = action.execute(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		else if (command != null && command.trim().equals("List.Quest")) {

			action = new QuestListAction();
			try {

				forward = action.execute(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		 /*
		else if (command != null && command.trim().equals("Info.Quest")){
			action = new QuestInfoAction();
			try {

				forward = action.execute(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}*/

		else if (command != null && command.trim().equals("ModifyAction.Quest")){
			action = new QuestModifyAction();
			try {

				forward = action.execute(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		if (forward != null) {
			if (forward.isRedirect()) {
				response.sendRedirect(forward.getPath());

			} else {
				RequestDispatcher dispatcher = request
						.getRequestDispatcher(forward.getPath());
				System.out.println(dispatcher.toString());

				dispatcher.forward(request, response);
			}
		}
	}
}
